qx.Class.define("caraffa.widgets.ListaPlanillasOS",
{
extend : qx.ui.core.Widget,
construct : function (){
	this.base(arguments);
	this._setLayout(new qx.ui.layout.Basic());
	
	//RPC
    this.rpcPlanillas = new qx.io.remote.Rpc();
	this.rpcPlanillas.setTimeout(10000);
	this.rpcPlanillas.setUrl("services/");
	this.rpcPlanillas.setServiceName("listaPlanillasOS");

	this._cmbOSociales = new qx.ui.form.ow.ComboFilter("Obra Social:");
	this._cmbTipo = new qx.ui.form.ow.ComboBox("Tipo:",[{value:"T", label:"Todas"},{value:"N", label:"Nuevas"},{value:"C", label:"Cobradas"}], false);
		this._cmbTipo.getLabel().setWidth(0);
	this._mdlPlanillas = new qx.ui.table.model.Simple();
		this._mdlPlanillas.setColumns(["Nro Planilla", "Fecha", "Obra Social", "Plan", "Nro. Factura", "Tipo", "Importe"]);
	this._tblPlanillas = new qx.ui.table.Table(this._mdlPlanillas);
		this._tblPlanillas.setStatusBarVisible(true);
		this._tblPlanillas.setColumnWidth(0, 80);
			//this._tblPlanillas.getTableColumnModel().setColumnVisible(0,false);
		this._tblPlanillas.setColumnWidth(1, 80);
		this._tblPlanillas.setColumnWidth(2, 140);
		this._tblPlanillas.setColumnWidth(3, 200);
		this._tblPlanillas.setColumnWidth(4, 100);
		this._tblPlanillas.setColumnWidth(5, 80);
		this._tblPlanillas.setColumnWidth(6, 100);
		this._tblPlanillas.set({width:800,height:300});
		var rndCajaMensual = new qx.ui.table.cellrenderer.Number("right");
	this._grpBotonera = new qx.ui.groupbox.GroupBox();
		this._grpBotonera.setLayout(new qx.ui.layout.HBox(30).set({alignX:"center"}));
		this._grpBotonera.setWidth(740);
		this._btnImprimir = new qx.ui.form.Button("Imprimir Planilla", "caraffa/22/impresora2.png");
		this._btnImprimirTot = new qx.ui.form.Button("Imprimir Totales", "caraffa/22/impresora2.png");
		this._btnDeshacer = new qx.ui.form.Button("Deshacer Planilla");
		this._btnAsignarFactura = new qx.ui.form.Button("Asignar Nro y Tipo Factura")
	this._grpBotonera.add(this._btnImprimir);
	this._grpBotonera.add(this._btnImprimirTot);
	this._grpBotonera.add(this._btnDeshacer);
	this._grpBotonera.add(this._btnAsignarFactura);

	this._add(this._cmbOSociales,{top:10,left:10});	
	this._add(this._cmbTipo,{top:10,left:380});
	this._add(this._tblPlanillas,{top:40,left:10});
	this._add(this._grpBotonera,{top:350,left:10});
	this._attachEvents();

	this._initForm();
},
members :{
	
	_attachEvents: function(){
		this._initForm;
		this._cmbOSociales.addListener('changeValue', this._cmbOSocialesChangeValue, this);
		this._btnImprimir.addListener('execute', this._btnImprimirExecute, this);
		this._btnImprimirTot.addListener('execute', this._btnImprimirTotales, this);
		this._cmbTipo.addListener('changeValue', this._cmbTipoChangeValue, this);
		this._btnDeshacer.addListener("execute", function () {
			if (this._mdlPlanillas.getValue(0, this._tblPlanillas.getFocusedRow())) {
				if (confirm("Esta seguro que desea Deshacer la planilla seleccionada?")) {
					var rpc = new qx.io.remote.Rpc("services/", "listaPlanillasOS");
					var p = {};
					p.cod_planilla_cobro = this._mdlPlanillas.getValue(0, this._tblPlanillas.getFocusedRow());
					rpc.callSync("DeshacerPlanilla", p);
					this._tblPlanillasAppear();
				}
			}
		}, this);
		
		this._btnAsignarFactura.addListener("execute", function () {
			if (this._mdlPlanillas.getValue(0, this._tblPlanillas.getFocusedRow())) {
				var p = {};
				p.nro_factura = prompt("Ingrese el Nro de Factura:", "");
				p.tipo_factura = prompt("Ingrese el Tipo de Factura: (A o B)", "");
				p.cod_planilla_cobro = this._mdlPlanillas.getValue(0, this._tblPlanillas.getFocusedRow());
				if ((p.nro_factura) && (p.tipo_factura)) {
					var rpc = new qx.io.remote.Rpc("services/", "listaPlanillasOS");
					rpc.callSync("NroTipoFactura", p);
					this._tblPlanillasAppear();
				}
			}
		}, this);
	},
	_initForm: function(){
		this._cmbOSocialesAppear();
	},
	_tblPlanillasAppear: function(){
		try{
			var params = new Object();
				params.cod_obra_social = this._cmbOSociales.getValue();
				params.tipo = this._cmbTipo.getValue();
			var result = this.rpcPlanillas.callSync("getPlanillas", params);
			this._mdlPlanillas.setData(result.values);
		}
		catch(ex){
			alert("Error: " + ex);
		}
	},
	_cmbOSocialesChangeValue: function(){
		this._tblPlanillasAppear();
	},	
	_cmbOSocialesAppear: function(){
		try{
			var result = this.rpcPlanillas.callSync("getOSociales");
			this._cmbOSociales.setNewValues(result, false);
		}
		catch(ex){
			alert("Error: " + ex);
		}	
	},
	_cmbTipoChangeValue: function(){
		this._tblPlanillasAppear();
	},	
	_btnImprimirExecute: function(){
		var params = new Object();
			if(this._tblPlanillas.getSelectionModel().getSelectedCount()){
				params.cod_planilla_cobro = this._mdlPlanillas.getValue(0, this._tblPlanillas.getFocusedRow());
				var impresion = this.rpcPlanillas.callSync("printPlanilla", params);
				window.open(impresion);

			}
			else{
				alert("Debe seleccionar una planilla");
			}
	},
	_btnImprimirTotales: function(){
		var params = new Object();
		params.cod_obra_social = this._cmbOSociales.getValue();
		params.tipo = this._cmbTipo.getValue();
		var impresionTotales = this.rpcPlanillas.callSync("printTotales", params);
		window.open(impresionTotales);
	}
}
});
